from django.contrib.auth.models import User
from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets
from rest_framework.permissions import AllowAny, IsAuthenticatedOrReadOnly

from user_info.permissions import IsSelfOrReadOnly
from user_info.serializers import UserRegisterSerializer


class UserViewSet(viewsets.ModelViewSet):
    """用户视图集"""
    queryset = User.objects.all()
    serializer_class = UserRegisterSerializer
    lookup_field = 'username'

    def get_permissions(self):
        if self.request.method == 'POST':  # 用户注册，所有人都可以
            self.permission_classes = [AllowAny]
        else:  # 只有本人可以进行修改，删除等操作
            self.permission_classes = [IsAuthenticatedOrReadOnly, IsSelfOrReadOnly]
        return super().get_permissions()
